/**
 * 
 */
package com.onlinemathuracrud.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.onlinemathuracrud.configurations.ApplicationConstants;
import com.onlinemathuracrud.dao.CityDao;
import com.onlinemathuracrud.dao.TempleDao;
import com.onlinemathuracrud.model.Image;
import com.onlinemathuracrud.model.Temple;
import com.onlinemathuracrud.service.TempleService;

/**
 * @author Amit Kashyap
 * @createdDate Sep 14, 2013
 * @createdTime 7:11:30 PM
 */
@Service
@Transactional(readOnly = true)
public class TempleServiceImpl implements TempleService {
	 
		@Autowired
		TempleDao templeDao;
		
		@Autowired
		CityDao cityDao;
	  
	    
		@Transactional(readOnly = false)
	    public void save(Temple temple) {
	    	List<MultipartFile> imageFiles = temple.getImageFiles();
			
			if(temple.getId()==null)
			{
				temple.setInsertTime(new Date(System.currentTimeMillis()));
				templeDao.save(temple);
			}

			String filePath = ApplicationConstants.TEMPLE_MEDIA_STORE_LOCATION+"\\"+temple.getId()+"\\";
			File destinationDir = new File(filePath);
			if(!destinationDir.exists())
			{
			destinationDir.mkdir();
			}
			
			
			
			Set<Image> newTempleImages = null;
			if (null != imageFiles && imageFiles.size() > 0) {
				newTempleImages = new HashSet<Image>();
				for (MultipartFile multipartFile : imageFiles) {
					if(!multipartFile.getOriginalFilename().trim().equals(""))
					{
					String fileName = multipartFile.getOriginalFilename();
					File destination = new File(filePath+fileName);
					try {
						multipartFile.transferTo(destination);
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Image img = new Image();
					img.setInsertTime(new Date(System.currentTimeMillis()));
					img.setCity(temple.getCity());
					img.setLocation(temple.getLocation());
					img.setName(fileName);
					img.setCity(temple.getCity());
					img.setMediaURI(filePath+fileName);
					newTempleImages.add(img);
					}
				}
			}

			Temple templeInDB = templeDao.findById(temple.getId());
			Set<Image> oldTempleImages = null;
			if(templeInDB != null)
			{
				//Hibernate.initialize(templeInDB.getTempleImages());
				oldTempleImages = templeInDB.getTempleImages();
			}
			
			//combining old and new temple images by adding new temple images to old images
			if(oldTempleImages != null && oldTempleImages.size()>0)
			{
				oldTempleImages.addAll(newTempleImages);
				temple.setTempleImages(oldTempleImages);
			}
			else
			{
				temple.setTempleImages(newTempleImages);
			}
				
				temple.setInsertTime(templeDao.findById(temple.getId()).getInsertTime());
				temple.setUpdateTime(new Date(System.currentTimeMillis()));
				templeDao.update(temple);
			
			

	    }
	 
	}